﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int find(int arr[], int n, int sz)
{
	int left = 0, right = sz - 1;

	while (left <= right)
	{
		int middle = left + (right - left) / 2;
		if (arr[middle] > n)
			right = middle - 1;
		else if (arr[middle] < n)
			left = middle + 1;
		else
			return middle;
	}

	return -1;
}

int main()
{
	int arr[] = { 0,1,2,3,4,5,6,7,8,9 };
	int n = 0;
	printf("请输入需要寻找的数字：");
	scanf("%d", &n);
	int sz = sizeof(arr) / sizeof(arr[0]);
	int a = find(arr, n, sz); //找到传下标，没找到传-1
	if (-1 == a)
		printf("没找到");
	else
		printf("找的了，%d的下标是%d", n, a);
	return 0;
}